<?php

class HighScores extends Controller {
	
	function HighScores()
	{
		parent::Controller();
		$this->load->helper('aes');
	}
	
	function trunk(){

		$this->db->query("TRUNCATE TABLE `score`");
		$this->db->query("TRUNCATE TABLE `speler`"); 
	
	}
	
	function get($param)
	{

		$this->db->select('Naam, Score, Email');
		$this->db->from('score');
		$this->db->join('speler', 'score.SpelersID = speler.ID');
		$this->db->order_by('score.Score', 'desc'); 
		$this->db->limit($param);
		
		$query = $this->db->get();
		foreach ($query->result() as $row)
		{
		    echo $row->Naam . " " . $row->Score . "\n";
		}
		
	}
	
	function getAll(){
		
		$this->db->from('speler');
		$query = $this->db->get();
	
		foreach ($query->result() as $row)
		{
		    echo $row->ID . " " . $row->Naam . " " . $row->EmailsOntvangen;
		}

		
	}
	
	function getClose($speler){
		
		$this->db->select('Naam, score.ID');
		$this->db->select_max('Score');
		$this->db->from('score');
		$this->db->join('speler', 'score.SpelersID = speler.ID');
		$this->db->order_by('Score', 'desc');
		$this->db->group_by('speler.ID');
	
		$this->db->where('speler.ID', $speler);
		$this->db->limit(1);
		
		$result = $this->db->get();
		
		if ($result->num_rows() > 0){
					
			$id = $result->row(0)->ID;
			
			$this->db->select('Naam, score.ID');
			$this->db->select_max('Score');
			$this->db->from('score');
			$this->db->join('speler', 'score.SpelersID = speler.ID');
			$this->db->order_by('Score', 'desc');
			$this->db->group_by('speler.ID');
			
			$result = $this->db->get();
			$rowindex = -1;
			
			for($i = 0; $i<$result->num_rows(); $i++){
				
				if ($result->row($i)->ID == $id){
					
					$rowindex = $i;
					
				}
			
			}
			
			$rowindex = $rowindex - 2;
			
			if(($rowindex) < 0) $rowindex = 0;
			if(($rowindex+5) > $result->num_rows()) $rowindex = $result->num_rows()-5;
			
			for($j = $rowindex; $j<($rowindex+5); $j++){
				
				if ($j >= 0){
				
					$resultArray[$j] = $result->row($j);

				}
				
			}
			
			if ($rowindex < 0){
				
				$data["firstPlace"] = 0;

			}else{
				
				$data["firstPlace"] = $rowindex;
				
			}
			
			$data["result"] = $resultArray;
			
		}		
		
		$data["query"] = null;
		$this->load->view('highscore_xml', $data);

		
	}
	
	/*function getClose($speler)
	{

		$this->db->select('Naam, Score, score.ID');
		$this->db->from('score');
		$this->db->join('speler', 'score.SpelersID = speler.ID');
		$this->db->order_by('score.Score', 'desc');
		$this->db->group_by('speler.ID');
		
		$this->db->where('speler.ID', $speler);
		$this->db->limit(1);
		
		$result = $this->db->get();
		
		if ($result->num_rows() > 0){
					
			$id = $result->row(0)->ID;
			
			$this->db->select('Naam, Score, score.ID');
			$this->db->from('score');
			$this->db->join('speler', 'score.SpelersID = speler.ID');
			$this->db->order_by('score.Score', 'desc');
			
			$result = $this->db->get();
			$rowindex = -1;
			
			for($i = 0; $i<$result->num_rows(); $i++){
				
				if ($result->row($i)->ID == $id){
					
					$rowindex = $i;
					
				}
			
			}
			
			$rowindex = $rowindex - 2;
			
			if(($rowindex) < 0) $rowindex = 0;
			if(($rowindex+5) > $result->num_rows()) $rowindex = $result->num_rows()-5;
			
			for($j = $rowindex; $j<($rowindex+5); $j++){
				
				if ($j >= 0){
				
					$resultArray[$j] = $result->row($j);

				}
				
			}
			
			if ($rowindex < 0){
				
				$data["firstPlace"] = 0;

			}else{
				
				$data["firstPlace"] = $rowindex;
				
			}
			
			$data["selected"] = $rowindex+2;
			$data["result"] = $resultArray;
			
		}		
		
		$data["query"] = null;
		$this->load->view('highscore_xml', $data);
		
	}*/
	
	function place(){
		 
		$ascii = base64_decode($_POST["encString"]);
		$data["speler"] = "-1";
		
		$val = dechex(ord(substr($ascii, 0, 1)));
		$hex = str_repeat("0", 2-strlen($val)).$val;
		for($i = 1; $i < strlen($ascii); $i = $i + 1) {
		  $val = dechex(ord(substr($ascii, $i, 1)));
		  $hex .= str_repeat("0", 2-strlen($val)).$val;
		}
		
		$rawPostData = decrypt($hex, "8b515416e6372872ac18d80791b70b81");
		
		$rawPostData = preg_replace("/[^a-zA-Z0-9\|@._\s-]/", "", $rawPostData);
		$data["decoded"] = $rawPostData;
		
		if(preg_match("/^[a-zA-Z\s]+[\|]{1}([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)[\|]{1}(true|false)[\|]{1}[0-9-]+$/", $rawPostData)){
		
			try
			{
				
				$receivedInfo = explode("|", $rawPostData);

				if ($receivedInfo[2] == "true"){
				
					$bool = 1;
					
				}else{

					$bool = 0;
				
				}
				
				$speler = array(
			               'Naam' => $receivedInfo[0],
			               'Email' => $receivedInfo[1],
			               'EmailsOntvangen' => $bool,
		            	  );
				
				$this->db->where('Email', $speler["Email"]);
				$this->db->from('speler');
				$this->db->limit(1);
				$result = $this->db->get();
				
				$id = 0;
				
				if ($result->num_rows() > 0){
					
					$id = $result->row(0)->ID;
					
					$data = array(
		               'Naam' => $receivedInfo[0],
			           'EmailsOntvangen' => $bool,
		            );
				
					$this->db->where('ID', $id);
					$this->db->update('speler', $data); 
					
				}else{
					
					$this->db->insert("speler", $speler);
					$id = $this->db->insert_id();

				}
				
				$now = date("Y-m-d H:i:s");
				
				$score = array(
			               	'Datum' 	=> $now,
			               	'Score' 	=> $receivedInfo[3],
			               	'Tijd' 		=> 0,
							'IP'		=> $_SERVER['REMOTE_ADDR'],
							'SpelersID' => $id,
		            	  );
		            	  
		        $this->db->insert("score", $score);
		
		        $data["speler"] = $id;
				$data["status"] = "OK";
				
			}
			catch (Exception $e)
			{
			   
				$data["status"] = "UNKNOWN_ERROR";
				
			}
			
		}else{
			
			$data["status"] = "NOT_WELL_FORMED";
			
		}
		
		$this->load->view('highscorestatus_xml', $data);
		
	}
	
}